new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      form: {
        System_SerialNumber: '',
        System_Name: '',
        Picture_URL: '',
        System_Remark: '',
      },
      loading: false
    }
  },
  mounted() {
    const System_SerialNumber = sessionStorage.getItem("System_SerialNumber")
    this.getData(System_SerialNumber);
  },
  methods: {
    getData(System_SerialNumber){
      const _this = this;
      $.ajax({
        url: DomainName + '/getInquiryIDOriginal?token='+ $.cookie('token'),
        type: 'POST',
        data:{
          Primary_Key:System_SerialNumber,
          Primary_Key_Name:'System_SerialNumber',
          Data:'system_info'
        },
        success: function (res) {
          _this.form = res[0]
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Data: JSON.stringify({ Modify: { system_info: [modify] } }) ,Operation_Name:'修改系统'}
          } else {
            data = {
              Data: JSON.stringify({ Modify: { system_info: [modify] }, Add: { system_info: [add] }}),
              Operation_Name:'修改系统'
            }
          }
          _this.loading = true
          $.ajax({
            url: DomainName + '/getEditAction?token='+$.cookie('token'),
            type: 'POST',
            data,
            success: function (res) {
              _this.$message.success("提交成功")
              window.history.go(-1);
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const modify = deepClone(this.form)
      return { add: null, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    uploadFront() {
      let _this = this
      let num = 1
      new Compressor(file, {
        quality: num,
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name,
                Path:'icon'
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.form.pic = e.target.result
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    }
  }
})